package com.three.lab.fragment;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.three.lab.R;
import com.three.lab.adapter.ClassifyAdapter;
import com.three.lab.adapter.ClassifyChildAdapter;
import com.three.lab.base.AppConstant;
import com.three.lab.base.BaseFragment;
import com.three.lab.bean.ClassifyBean;
import com.three.lab.bean.ClassifyChildBean;
import com.three.lab.bean.FootPrintBean;
import com.three.lab.bean.GoodsBean;
import com.three.lab.page.goods.GoodsListActivity;
import com.three.lab.page.message.OfficeMsgDetailActivity;
import com.three.lab.widget.SearchView;

import java.util.ArrayList;
import java.util.List;

public class ClassifyFragment extends BaseFragment {

    private SearchView searchView;
    private TextView tvLoading;
    private View mLoadingView;
    private ImageView ivState;
    private ProgressBar progressBar;
    private RecyclerView rvLeft,rvRight;
    private ClassifyAdapter classifyAdapter;
    private ClassifyChildAdapter adapter;

    private int pageNo = 1;
    private boolean isloadMore = false;

    public static ClassifyFragment newInstance(int index, String title) {
        ClassifyFragment fragment = new ClassifyFragment();
        Bundle args = new Bundle();
        args.putInt(AppConstant.INDEX, index);
        args.putString(AppConstant.INDEX_NAME, title);
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    protected void initArguments(Bundle bundle) {

    }

    @Override
    protected int bindLayout() {
        return R.layout.fragment_classify;
    }

    @Override
    protected void initViews(View view, Bundle savedInstanceState) {
        searchView = $(R.id.search_bar);
        rvLeft = $(R.id.rv_left);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        rvLeft.setLayoutManager(linearLayoutManager);
        classifyAdapter = new ClassifyAdapter();
        rvLeft.setAdapter(classifyAdapter);

        mLoadingView = LayoutInflater.from(getActivity()).inflate(R.layout.loading_view, null);
        ivState = mLoadingView.findViewById(R.id.iv_state);
        tvLoading = mLoadingView.findViewById(R.id.tv_loading);
        progressBar = mLoadingView.findViewById(R.id.progressBar);

        rvRight = $(R.id.rv_right);
        LinearLayoutManager layoutManagerRight = new LinearLayoutManager(getActivity());
        rvRight.setLayoutManager(layoutManagerRight);
        adapter = new ClassifyChildAdapter();
        rvRight.setAdapter(adapter);
    }

    @Override
    public void initListener() {
        searchView.setSearchListener(new SearchView.OnSearchChangeListener() {
            @Override
            public void onSearchChanged(String keyWord) {

            }

            @Override
            public void onTextChanged(String keyWord) {

            }
        });
        classifyAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter baseQuickAdapter, View view, int position) {
                classifyAdapter.setSelPosition(position);
                baseQuickAdapter.notifyDataSetChanged();
                LinearLayoutManager layoutManagerRight = (LinearLayoutManager) rvRight.getLayoutManager();
                if (layoutManagerRight != null) {
                   // rvRight.smoothScrollToPosition(position);//平滑过渡
                    layoutManagerRight.scrollToPositionWithOffset(position,0);//定位到某个item，并将其顶部显示
                }
            }
        });
        adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter baseQuickAdapter, View view, int position) {
                ClassifyChildBean bean = (ClassifyChildBean) baseQuickAdapter.getItem(position);
                switch (view.getId()){
                    case R.id.ll_more:
                        Bundle bundle = new Bundle();
                        bundle.putParcelable(AppConstant.EXTRAS_DATA,bean);
                        startActivity(GoodsListActivity.class,bundle);
                        break;
                }
            }
        });
        rvRight.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }

            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
                LinearLayoutManager layoutManagerLeft = (LinearLayoutManager) rvLeft.getLayoutManager();
//                if (layoutManagerLeft != null && layoutManager != null) {
//                    int firstVisibleItemPos = layoutManager.findFirstVisibleItemPosition();
//                    int position = 0 ;
//                    if (layoutManagerLeft.findFirstVisibleItemPosition() > firstVisibleItemPos) {
//                        layoutManagerLeft.scrollToPositionWithOffset(firstVisibleItemPos,0);
//                    } else if (layoutManagerLeft.findFirstVisibleItemPosition() < firstVisibleItemPos) {
//                        layoutManagerLeft.scrollToPositionWithOffset(firstVisibleItemPos,0);
//                    }
//                    if (!rvRight.canScrollVertically(1)) {
//                        firstVisibleItemPos = classifyAdapter.getData().size() - 1;
//                    }
//                    classifyAdapter.setSelPosition(firstVisibleItemPos);
//                    classifyAdapter.notifyDataSetChanged();
//                }
            }
        });
    }

    @Override
    public void widgetClick(View v) {

    }

    @Override
    protected void initData(Context context) {
        onQueryClassifyData();
    }

    private void onQueryClassifyData() {
        progressBar.setVisibility(View.VISIBLE);
        tvLoading.setText(getString(R.string.loading));
        ivState.setVisibility(View.GONE);
        adapter.setEnableLoadMore(true);
        adapter.setEmptyView(mLoadingView);

        List<ClassifyBean> classifyBeanList = new ArrayList<>();
        List<ClassifyChildBean> classifyChildBeans = new ArrayList<>();

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 30; i++) {
                    ClassifyBean bean = new ClassifyBean(i,"分类" + i,false);
                    classifyBeanList.add(bean);

                    ClassifyChildBean childBean = new ClassifyChildBean();
                    childBean.setPid(i);
                    childBean.setPname("分类" + i);
                    childBean.setId(100 + i);

                    List<GoodsBean> goodsBeans = new ArrayList<>();
                    for (int j = 0; j < 10; j++) {
                        GoodsBean goodsBean = new GoodsBean();
                        goodsBean.setId(1000 + j);
                        goodsBean.setName("商品" + j);
                        goodsBean.setGoodsUrl("https://m.zhaokaobao.com.cn/uploads/202007/fd4222c22333217927dfc9c68146f0f0.jpg");
                        goodsBeans.add(goodsBean);
                    }
                    childBean.setGoodsBeanList(goodsBeans);

                    classifyChildBeans.add(childBean);
                }
                classifyAdapter.setNewData(classifyBeanList);
                setClassifyChildAdapter(classifyChildBeans);//设置页面数据
            }
        }, 1000);
    }

    private void setClassifyChildAdapter(List<ClassifyChildBean> mlist) {
        if (isloadMore) {
            adapter.addData(mlist);
            if (mlist.size() < AppConstant.DEFAULT_PAGE_SIZE) {
                adapter.loadMoreEnd();
            } else {
                adapter.loadMoreComplete();
                adapter.setEnableLoadMore(true);
            }
        } else if (mlist.isEmpty()) {
            adapter.loadMoreEnd();
            adapter.setEnableLoadMore(false);
            setState(getString(R.string.no_data), R.mipmap.ic_empty);
        } else {
            adapter.setNewData(mlist);
            adapter.loadMoreComplete();
            adapter.setEnableLoadMore(true);
            isloadMore = false;
            adapter.loadMoreEnd();
        }
    }

    private void setState(String string, int resId) {
        progressBar.setVisibility(View.GONE);
        tvLoading.setText(string);
        ivState.setImageResource(resId);
        ivState.setVisibility(View.VISIBLE);
        adapter.setEmptyView(mLoadingView);
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        progressBar.setVisibility(View.GONE);
    }
}
